package cn.jiedh.app.meiweidk.Utils.tool;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.UUID;

/**
 * @name：
 * @author： 杨 广
 * @phone： 17382373271
 * @createTime： 2017/2/16
 * @modifyTime： 2017/2/16
 * @explain：
 */

public class DeviceUtils {


	/**
	 * 获取应用程序的IMEI号
	 */
	public static String getIMEI(Context context) {
		String imei = "";
		String[] permissions = {Manifest.permission.READ_PHONE_STATE};
		int check = ContextCompat.checkSelfPermission(context, permissions[0]);
		if (check == PackageManager.PERMISSION_GRANTED) {
			TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			imei = telephonyManager.getDeviceId();
		} else {
			T.showShort("请开启获取手机信息权限");
		}
		return imei;
	}
	/**
	 * 获取应用程序的IMSI号
	 */
	@SuppressLint("MissingPermission")
	public static String getIMSI(Context context) {
		TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi = mTelephonyMgr.getSubscriberId();

		return imsi ;
	}
	/**
	 * 获取应用程序的UUID号
	 */
	@SuppressLint("MissingPermission")
	public static String getUUID(Context context){
		final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String tmDevice="";
		final String androidId;
		try{
			tmDevice = "" + tm.getDeviceId();
		}catch (Exception e){
		}
		androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
		UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32));
		String uniqueId = deviceUuid.toString();
		return uniqueId;
	}

	/**
	 * 获取设备品牌
	 */
	public static String getDeviceBRAND() {
		String brand = android.os.Build.BRAND;
		return brand;
	}


	/**
	 * 判断当前设备是手机还是平板，代码来自 Google I/O App for Android
	 * @param context Context
	 * @return 平板返回 True，手机返回 False
	 */
	public static boolean isTablet(Context context) {
		return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
	}


	/**
	 * 获取设备的型号
	 */
	public static String getDeviceName() {
		String model = android.os.Build.MODEL;
		return model;
	}

	/**
	 * 设备的唯一标识
	 */
	public static String getDeviceFINGERPRINT() {
		String model = android.os.Build.FINGERPRINT;
		return model;
	}

	/**
	 * 获取设备的系统版本号
	 */
	public static int getDeviceSDK() {
		int sdk = android.os.Build.VERSION.SDK_INT;
		return sdk;
	}

	/**
	 * 获取包名
	 */
	public static String getPackageName(Context context) {
		PackageInfo info;
		String packageNames = null;//当前版本的包名
		try {
			info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			packageNames = info.packageName;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return packageNames;
	}

	/**
	 * 获取当前应用程序的版本名称
	 */
	public static String getversionName(Context context) {
		PackageInfo info;
		String versionName = null;//当前应用的版本名称
		try {
			info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			versionName = info.versionName;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}

	/**
	 * 获取当前应用程序的版本号
	 */
	public static int getVersionCode(Context context) {
		PackageInfo info;
		int versionCode = 0;//当前版本的版本号
		try {
			info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			versionCode = info.versionCode;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionCode;
	}


	/**
	 * 获取ip地址
	 * @return
	 */
	public static String getHostIP() {

		String hostIp = null;
		try {
			Enumeration nis = NetworkInterface.getNetworkInterfaces();
			InetAddress ia = null;
			while (nis.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) nis.nextElement();
				Enumeration<InetAddress> ias = ni.getInetAddresses();
				while (ias.hasMoreElements()) {
					ia = ias.nextElement();
					if (ia instanceof Inet6Address) {
						continue;// skip ipv6
					}
					String ip = ia.getHostAddress();
					if (!"127.0.0.1".equals(ip)) {
						hostIp = ia.getHostAddress();
						break;
					}
				}
			}
		} catch (SocketException e) {
			Log.i("yao", "SocketException");
			e.printStackTrace();
		}
		return hostIp;
	}

}
